import creatorMain
import os
import random
import math
import time

import cv2 as cv
import numpy as np
import multiprocessing


def random_ring_circle_center(maxradius):
    x = random.randint(maxradius, creatorMain.width - 2 * maxradius)
    y = random.randint(maxradius, creatorMain.height - 2 * maxradius)
    return [x, y]


def draw_ring_circle(_j, file_, file_path_, res_file_):
    max_radius = random.randint(120, 180)
    min_radius = max_radius - 20
    center = random_ring_circle_center(max_radius)
    img_ = creatorMain.create_img_random()
    blcakcolor = random.randint(0, 128)
    whiteolor = random.randint(150, 255)
    # cv.circle(img_, (center[0], center[1]), max_radius, whiteolor, -1, cv.LINE_AA)
    # cv.circle(img_, (center[0], center[1]), min_radius, blcakcolor, -1, cv.LINE_AA)
    cv.circle(img_, (center[0], center[1]), max_radius, whiteolor, 5)
    cv.circle(img_, (center[0], center[1]), min_radius, whiteolor, 5)

    centerx = center[0] / creatorMain.height
    centery = center[1] / creatorMain.width
    dx = (2 * max_radius) / creatorMain.height
    dy = (2 * max_radius) / creatorMain.width

    centerx2 = center[0] / creatorMain.height
    centery2 = center[1] / creatorMain.width
    dx2 = (2 * min_radius) / creatorMain.height
    dy2 = (2 * min_radius) / creatorMain.width

    res_file_.write("0" + ' ')
    res_file_.write(str(centerx) + ' ')
    res_file_.write(str(centery) + ' ')
    res_file_.write(str(dx) + ' ')
    res_file_.write(str(dy) + ' ' + "\n")

    res_file_.write("0" + ' ')
    res_file_.write(str(centerx2) + ' ')
    res_file_.write(str(centery2) + ' ')
    res_file_.write(str(dx2) + ' ')
    res_file_.write(str(dy2) + ' ')

    file_.write("name: " + str(_j) + '.bmp')
    file_.write("    center_point: " + str(center[0]) + "," + str(center[1]))
    file_.write("    radius: " + str(max_radius) + "\n")
    file_.write("name: " + str(_j) + '.bmp')
    file_.write("    center_point: " + str(center[0]) + "," + str(center[1]))
    file_.write("    radius: " + str(min_radius) + "\n")

    ksize = (5, 5)
    sigma = 0
    creatorMain.add_salt_and_pepper_noise(img_, 0.5)
    blurred = cv.GaussianBlur(img_, ksize, sigma)
    cv.imwrite(file_path_, blurred)

    # creatorMain.show_img_imshow(img_)


def create_ring_circle(dir_nums, img_nums,  root_path):
    for i in range(1, dir_nums + 1):
        folder_path = os.path.join(root_path, str(i))
        res_path_d = os.path.join(folder_path, "res")
        txt_path = os.path.join(folder_path, str(i) + '.txt')
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)
        if not os.path.exists(res_path_d):
            os.makedirs(res_path_d)
        file = open(txt_path, 'w')
        for j in range(1, img_nums + 1):
            if not os.path.exists(folder_path):
                os.makedirs(folder_path)
            start_time = time.time()
            file_path = os.path.join(folder_path, str(j) + '.bmp')
            res_path = os.path.join(res_path_d, str(j) + ".txt")
            res_file = open(res_path, 'w')
            draw_ring_circle(j, file, file_path, res_file)
            end_time = time.time()
            print(j, "-->finish", "time  -->", end_time - start_time)


if __name__ == '__main__':
    root_dir = "D:/ring"
    dir_num = 1
    img_num = 5
    create_ring_circle(dir_num, img_num, root_dir)
